<?php

/**
 * This is the model class for table "{{good}}".
 *
 * The followings are the available columns in table '{{good}}':
 * @property string $id
 * @property integer $city_id
 * @property integer $user_id
 * @property string $token
 * @property string $title
 * @property string $thumb
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $listorder
 * @property integer $is_show
 * @property integer $belong_time
 * @property string $is_recommend
 * @property integer $click_num
 * @property integer $share_num
 */
class Good extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{good}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('city_id, user_id', 'required'),
			array('city_id, user_id, create_time, update_time, listorder, is_show, belong_time, click_num, share_num', 'numerical', 'integerOnly'=>true),
			array('token', 'length', 'max'=>20),
			array('title', 'length', 'max'=>200),
			array('thumb', 'length', 'max'=>120),
			array('is_recommend', 'length', 'max'=>12),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, city_id, user_id, token, title, thumb, create_time, update_time, listorder, is_show, belong_time, is_recommend, click_num, share_num', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'city_id' => 'City',
			'user_id' => 'User',
			'token' => 'Token',
			'title' => 'Title',
			'thumb' => 'Thumb',
			'create_time' => 'Create Time',
			'update_time' => 'Update Time',
			'listorder' => 'Listorder',
			'is_show' => 'Is Show',
			'belong_time' => 'Belong Time',
			'is_recommend' => 'Is Recommend',
			'click_num' => 'Click Num',
			'share_num' => 'Share Num',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('city_id',$this->city_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('token',$this->token,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('thumb',$this->thumb,true);
		$criteria->compare('create_time',$this->create_time);
		$criteria->compare('update_time',$this->update_time);
		$criteria->compare('listorder',$this->listorder);
		$criteria->compare('is_show',$this->is_show);
		$criteria->compare('belong_time',$this->belong_time);
		$criteria->compare('is_recommend',$this->is_recommend,true);
		$criteria->compare('click_num',$this->click_num);
		$criteria->compare('share_num',$this->share_num);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Good the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
    public function getList($city_id){
        $criteria = new CDbCriteria;
        $criteria->addCondition("city_id=$city_id");
        $criteria->order = "id DESC";
        $dataProvider= new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>20,
            ),
        ));
        return $dataProvider;
    }
    public function getSearchList($city_id,$keyword){
        $criteria = new CDbCriteria;
        $criteria->addCondition("city_id=$city_id");
        $criteria->addSearchCondition('title',$keyword,true,'AND');
        $criteria->order = "id DESC";
        $dataProvider= new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>20,
            ),
        ));
        return $dataProvider;
    }
    public function getFrontCountList($city_id,$getMonth){
        $db = Yii::app()->db->createCommand();
        $db->select('count(*) as count');
        $db->from('{{good}}');
        $db->andWhere('city_id=:city_id and is_show=:is_show and belong_time=:belong_time',array(':city_id'=>$city_id,':belong_time'=>$getMonth,':is_show'=>1));
        $info = $db->queryRow();
        return $info['count'];
    }
    public function getFrontList($city_id,$getMonth,$first,$total){
        $db = Yii::app()->db->createCommand();
        $db->select('title,token,thumb');
        $db->from('{{good}}');
        $db->andWhere('city_id=:city_id and is_show=:is_show and belong_time=:belong_time',array(':city_id'=>$city_id,':belong_time'=>$getMonth,':is_show'=>1));
        $db->order('listorder desc,update_time desc');
        $db->limit($total,$first);
        $info = $db->queryAll();
        return $info;
    }
    public function getFrontOne($city_id,$getMonth,$type){
        $db = Yii::app()->db->createCommand();
        $db->select('title,token,thumb');
        $db->from('{{good}}');
        $db->andWhere('city_id=:city_id and is_show=:is_show and belong_time=:belong_time and is_recommend=:is_recommend',array(':city_id'=>$city_id,':belong_time'=>$getMonth,':is_recommend'=>$type,':is_show'=>1));
        $db->order('listorder desc,update_time desc');
        $info = $db->queryRow();
        return $info;
    }



}
